
package com.bear.cmcc.NetTestHTTP.ui.activity;

import com.bear.cmcc.NetTestHTTP.R;
import com.bear.cmcc.NetTestHTTP.component.VillageBean;
import com.bear.cmcc.NetTestHTTP.component.json.respond.CityAreaFtpInfoRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.GetNetDataReturnInfoRespond;
import com.bear.cmcc.NetTestHTTP.component.json.respond.NetDataDescriInfoRespond;
import com.bear.cmcc.NetTestHTTP.constant.HttpConstant;
import com.bear.cmcc.NetTestHTTP.constant.MyCommonConstant;
import com.bear.cmcc.NetTestHTTP.httpUtil.HttpUtil;
import com.bear.cmcc.NetTestHTTP.ui.adapter.CellSortAdapter;
import com.bear.cmcc.NetTestHTTP.ui.view.DefineDialog;
import com.bear.cmcc.NetTestHTTP.ui.view.HVListView;
import com.bear.cmcc.NetTestHTTP.ui.view.LoadingDialog;
import com.bear.cmcc.NetTestHTTP.util.CountryUtil;
import com.bear.cmcc.NetTestHTTP.util.MyCommonUtil;
import com.bear.cmcc.NetTestHTTP.util.SortCellIntSpeed;
import com.bear.cmcc.NetTestHTTP.util.SortCellStringSpeed;

import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class CellInfoActivity extends AbstractActivity {

    private CheckBox cbInfoSelect, cbSelect;

    private TextView tvSpeed, tvDropRate;

    private TextView tvCellName, tvSignalValue, tvjietong;

    private boolean isname, isCal, isFre, isDrop, isSignal, isRand;

    private boolean isShare, isShareData;

    private String cityName;

    private String cityCode;

    private boolean isFromGrid;

    private String gridCode;

    private String gridName;

    // private CountryBean mCountryBean;

    private ArrayList<VillageBean> itemlist = new ArrayList<VillageBean>();

    private ArrayList<VillageBean> cellListTD_good;

    //
    private ArrayList<VillageBean> cellList2G_good;

    //
    // private ArrayList<VillageBean> cellListTD_fall;
    //
    // private ArrayList<VillageBean> cellList2G_fall;

    private CellSortAdapter cityAdapter;

    private DefineDialog defineDialog;

    private Button btnNetTD, btnNet2G;

    private LinearLayout lyStretch, ly_first;

    private TextView allInfoTV, firstcontentTV;

    String showAllInfo;

    private HVListView listViewRank;

    private boolean isShowTdData = true;

    int clear = 3;

    int reshow = 2;

    int showDialog = 4;

    int total_size_td = HttpConstant.TOTAL_COUNT;

    int total_size_2g = HttpConstant.TOTAL_COUNT;

    Integer service_total_size_td = 0;

    Integer service_total_size_2g = 0;

    int every_size = HttpConstant.TOTAL_COUNT;

    private LoadingDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gridName = getIntent().getStringExtra(MyCommonConstant.GRID_NAME);
        cityName = getIntent().getStringExtra(MyCommonConstant.CITY_NAME);
        cityCode = getIntent().getStringExtra(MyCommonConstant.CITY_CODE);

        isFromGrid = getIntent().getBooleanExtra(MyCommonConstant.FROM_GRID, false);
        gridCode = getIntent().getStringExtra(MyCommonConstant.GRID_CODE);

        dialog = new LoadingDialog(this);

        loadBottomBar();
        choseBottomItemIndex(MyCommonConstant.COUNTRY_ACTIVITY_INDEX);
        if (isFromGrid) {
            setTitle(gridName);
        } else {
            setTitle(cityName);
        }
        setContentView(R.layout.activity_cell_net_new);
        layoutInit();
        dataInit();
    }

    @Override
    protected void onStart() {
        choseBottomItemIndex(MyCommonConstant.COUNTRY_ACTIVITY_INDEX);
        super.onStart();
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    private void layoutInit() {
        setCloseButton(R.string.top_back, onclick);
        setShareButton(onclick);

        cbInfoSelect = (CheckBox) findViewById(R.id.cbInfoSelect);
        cbInfoSelect.setOnCheckedChangeListener(onCheckedListener);
        cbInfoSelect.setChecked(true);

        cbSelect = (CheckBox) findViewById(R.id.cbSelect);
        cbSelect.setOnCheckedChangeListener(onCheckedListener);
        tvCellName = (TextView) findViewById(R.id.tvCellName);
        // tvCalValue = (TextView) findViewById(R.id.tvCalValue);
        tvSpeed = (TextView) findViewById(R.id.tvSpeed);
        tvDropRate = (TextView) findViewById(R.id.tvDropRate);
        tvSignalValue = (TextView) findViewById(R.id.tvSignalValue);
        tvjietong = (TextView) findViewById(R.id.tvjietong);
        tvSpeed.setText(getString(R.string.rate) + getString(R.string.sortdown));
        tvSpeed.setOnClickListener(onclick);
        tvCellName.setOnClickListener(onclick);
        // tvCalValue.setOnClickListener(onclick);
        tvDropRate.setOnClickListener(onclick);
        tvSignalValue.setOnClickListener(onclick);
        tvjietong.setOnClickListener(onclick);

        listViewRank = (HVListView) findViewById(R.id.listViewRank);
        listViewRank.setmListHead((LinearLayout) findViewById(R.id.lyInfo));
        listViewRank.setOnItemClickListener(onItemClickListener);
        listViewRank.setOnScrollListener(scrollListener);
        cityAdapter = new CellSortAdapter(this, itemlist, listViewRank);
        listViewRank.setAdapter(cityAdapter);

        listViewRank.setOnScrollListener(myOnScrollListener);

        defineDialog = new DefineDialog(this);

        btnNetTD = (Button) findViewById(R.id.btnNetTD1);
        btnNetTD.setBackgroundResource(R.drawable.tabhead_actived_bak);
        // tvSignalValue.setText(String.valueOf(NetworkTestApp.getNetStrong()));
        btnNetTD.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (cellListTD_good == null || cellListTD_good.size() == 0) {
                    isShowTdData = true;
                    getData();
                } else {
                    isShowTdData = true;
                    handler.sendEmptyMessage(reshow);
                }

            }
        });
        btnNet2G = (Button) findViewById(R.id.btnNet2G1);
        btnNet2G.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (cellList2G_good == null || cellList2G_good.size() == 0) {
                    isShowTdData = false;
                    getData();
                } else {
                    isShowTdData = false;
                    handler.sendEmptyMessage(reshow);
                }

            }
        });

        lyStretch = (LinearLayout) findViewById(R.id.lyStretch);
        ((CheckBox) findViewById(R.id.cbStretch)).setOnCheckedChangeListener(onCheckedListener);
        ((LinearLayout) findViewById(R.id.ly_layout)).setOnClickListener(onClickListener);

        ly_first = (LinearLayout) findViewById(R.id.ly_first);
        allInfoTV = (TextView) findViewById(R.id.allcontent);
        firstcontentTV = (TextView) findViewById(R.id.firstcontent);
        firstcontentTV.setOnClickListener(onClickListener);
        allInfoTV.setOnClickListener(onClickListener);

    }

    private void shareLayout() {
        if (isShare) {
            cbInfoSelect.setVisibility(View.VISIBLE);
            cbSelect.setVisibility(View.VISIBLE);
            cityAdapter.setSelect(isShare);
        } else {
            cbInfoSelect.setVisibility(View.GONE);
            cbSelect.setVisibility(View.GONE);
            cityAdapter.setSelect(isShare);
        }
    }

    private void dataInit() {
        isShare = false;
        shareLayout();
        getData();
    }

    private void setLayoutData() {
        allInfoTV.setText(Html.fromHtml(showAllInfo));
        firstcontentTV.setText(Html.fromHtml(showAllInfo));
    }

    private void initializeShowAllInfo(boolean isFromGrid) {
        int allDevice = 0;
        int cellNumber = 0;
        int gridNumber = 0;
        int randTd = 0;
        int rand2G = 0;
        String speedTd = "0";
        String speed2G = "0";

        String showName = cityName;

        if (isFromGrid) {
            showName = gridName;
            GetNetDataReturnInfoRespond gridInfo = HttpUtil.getGridNetInfo(CellInfoActivity.this,
                    cityCode, gridCode);

            if (gridInfo != null && gridInfo.getErrorcode() == HttpConstant.CODE_SUCCESS) {
                NetDataDescriInfoRespond respond = gridInfo.getData();
                if (respond != null) {
                    allDevice = respond.getOnline2G() + respond.getOnlineTD();
                    cellNumber = respond.getCellNumber();
                    gridNumber = respond.getGridNumber();
                    speedTd = respond.getAverageSpeedTD();
                    speed2G = respond.getAverageSpeed2G();
                    randTd = respond.getRandTd();
                    rand2G = respond.getRand2G();
                }
            }
        } else {

            GetNetDataReturnInfoRespond info = HttpUtil.getRegionNetInfo(CellInfoActivity.this,
                    HttpConstant.CITY, cityCode);

            if (info != null && info.getErrorcode() == HttpConstant.CODE_SUCCESS) {
                NetDataDescriInfoRespond respond = info.getData();
                if (respond != null) {
                    allDevice = respond.getOnline2G() + respond.getOnlineTD();
                    cellNumber = respond.getCellNumber();
                    gridNumber = respond.getGridNumber();
                    speedTd = respond.getAverageSpeedTD();
                    speed2G = respond.getAverageSpeed2G();
                    randTd = respond.getRandTd();
                    rand2G = respond.getRand2G();
                }
            }
        }

        showAllInfo = "&nbsp;&nbsp;&nbsp;&nbsp;"
                + CountryUtil.getSpecialGrayStr(this, showName
                        + getString(R.string.current_device_num))
                + CountryUtil.getSpecialWhiteStr(this, allDevice + "")
                + CountryUtil.getSpecialGrayStr(this, getString(R.string.unit));

        if (!isFromGrid) {
            showAllInfo = showAllInfo + CountryUtil.getSpecialWhiteStr(this, "" + gridNumber);
            showAllInfo = showAllInfo
                    + CountryUtil.getSpecialGrayStr(this, getString(R.string.current_city_wg));
        }

        showAllInfo = showAllInfo
                + CountryUtil.getSpecialWhiteStr(this, "" + cellNumber)
                + CountryUtil.getSpecialGrayStr(this, getString(R.string.current_city_xq))
                + CountryUtil.getSpecialGrayStr(this, getString(R.string.current_city_td)
                        + getString(R.string.current_city_qgpm))
                + CountryUtil.getSpecialWhiteStr(this, randTd + "")
                + CountryUtil.getSpecialGrayStr(this, getString(R.string.current_city_w))
                // + CountryUtil.getSpecialGrayStr(this,
                // getString(R.string.current_cell_qspm))
                // + CountryUtil.getSpecialWhiteStr(this, randTd+"")
                // + CountryUtil.getSpecialGrayStr(this,
                // getString(R.string.current_city_w))
                + CountryUtil.getSpecialGrayStr(this, getString(R.string.current_city_pjsl))
                + CountryUtil.getSpecialWhiteStr(this, speedTd + "")
                + CountryUtil.getSpecialGrayStr(this, getString(R.string.speedunit))
                // + CountryUtil.getSpecialGrayStr(this,
                // getString(R.string.yhbs))
                // + CountryUtil.getSpecialWhiteStr(this,
                // getString(R.string.yhbdvalue1))
                // + CountryUtil.getSpecialGrayStr(this,
                // getString(R.string.qsd))
                // + CountryUtil.getSpecialWhiteStr(this,
                // getString(R.string.qsvalue1))
                // + CountryUtil.getSpecialGrayStr(this,
                // getString(R.string.yhbe))
                + CountryUtil.getSpecialGrayStr(this, getString(R.string.current_city_2G)
                        + getString(R.string.current_city_qgpm))
                + CountryUtil.getSpecialWhiteStr(this, rand2G + "")
                + CountryUtil.getSpecialGrayStr(this, getString(R.string.current_city_w))
                // + CountryUtil.getSpecialGrayStr(this,
                // getString(R.string.current_cell_qspm))
                // + CountryUtil.getSpecialWhiteStr(this, "1")
                // + CountryUtil.getSpecialGrayStr(this,
                // getString(R.string.current_city_w))
                + CountryUtil.getSpecialGrayStr(this, getString(R.string.current_city_pjsl))
                + CountryUtil.getSpecialWhiteStr(this, speed2G + "")
                + CountryUtil.getSpecialGrayStr(this, getString(R.string.speedunit))
                // + CountryUtil.getSpecialGrayStr(this,
                // getString(R.string.yhbs))
                // + CountryUtil.getSpecialWhiteStr(this,
                // getString(R.string.yhbdvalue2))
                // + CountryUtil.getSpecialGrayStr(this,
                // getString(R.string.qsd))
                // + CountryUtil.getSpecialWhiteStr(this,
                // getString(R.string.qsvalue2))
                // + CountryUtil.getSpecialGrayStr(this,
                // getString(R.string.yhbe))
                + "<br/>"
                + CountryUtil.getSpecialGrayStr(this, showName
                        + getString(R.string.cellStretchTitleTDXQ));

        if (cellListTD_good != null && cellListTD_good.size() > 0) {

            Collections.sort(cellListTD_good, new Comparator<VillageBean>() {

                @Override
                public int compare(VillageBean lhs, VillageBean rhs) {
                    int result = lhs.getAverageSpeedInt() - rhs.getAverageSpeedInt();

                    return result;
                }

            });

            StringBuffer buff = new StringBuffer();
            int index = 0;
            for (VillageBean bean : cellListTD_good) {
                if (index >= 10) {
                    break;
                }
                buff.append(bean.getLac() + ",");
                index++;
            }

            if (buff.length() > 0) {
                buff = buff.delete(buff.length() - 1, buff.length());
            }
            showAllInfo = showAllInfo + CountryUtil.getSpecialGrayStr(this, buff.toString());
        }
    }

    private void setListViewData(boolean isTD) {
        try {
            if (isTD) {
                isShowTdData = true;
                btnNetTD.setBackgroundResource(R.drawable.tabhead_actived_bak);
                btnNet2G.setBackgroundResource(R.drawable.tabtail_normal_bak);
                if (cellListTD_good != null) {
                    itemlist.addAll(cellListTD_good);
                }
            } else {
                isShowTdData = false;
                btnNetTD.setBackgroundResource(R.drawable.tabhead_normal_bak);
                btnNet2G.setBackgroundResource(R.drawable.tabtail_actived_bak);
                if (cellList2G_good != null) {
                    itemlist.addAll(cellList2G_good);
                }
            }
            if (itemlist != null) {

                Collections.sort(itemlist, new Comparator<VillageBean>() {

                    @Override
                    public int compare(VillageBean lhs, VillageBean rhs) {
                        int result = lhs.getAverageSpeedInt() - rhs.getAverageSpeedInt();

                        return result;
                    }

                });

            }

            if (isTD) {
                if (total_size_td > every_size) {
                    listViewRank.setSelection(total_size_td - every_size);
                }
            } else {
                if (total_size_2g > every_size) {
                    listViewRank.setSelection(total_size_2g - every_size);
                }
            }

            cityAdapter.notifyDataSetChanged(itemlist.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void getData() {
        new Thread() {
            public void run() {

                handler.sendEmptyMessage(showDialog);
                boolean isSuccess = false;

                if (isFromGrid) {
                    if (isShowTdData) {

                        CityAreaFtpInfoRespond respond = CountryUtil.getGridFtpInfo(
                                CellInfoActivity.this, true, cityCode, true, gridCode,
                                total_size_td);
                        if (respond != null && respond.getErrorCode() == HttpConstant.CODE_SUCCESS) {
                            service_total_size_td = respond.getTotalCount();
                            cellListTD_good = CountryUtil.changeToVillageBean(respond);
                            total_size_td = cellListTD_good.size();
                            isSuccess = true;
                        }

                    } else {

                        CityAreaFtpInfoRespond respond = CountryUtil.getGridFtpInfo(
                                CellInfoActivity.this, true, cityCode, false, gridCode,
                                total_size_2g);
                        if (respond != null && respond.getErrorCode() == HttpConstant.CODE_SUCCESS) {
                            cellList2G_good = CountryUtil.changeToVillageBean(respond);
                            service_total_size_2g = respond.getTotalCount();
                            total_size_2g = cellList2G_good.size();
                            isSuccess = true;
                        }
                    }

                } else {

                    if (isShowTdData) {
                        CityAreaFtpInfoRespond respond = CountryUtil
                                .getVillageNetDataByCityCodeAdapter(CellInfoActivity.this, true,
                                        cityCode, true, total_size_td);
                        if (respond != null && respond.getErrorCode() == HttpConstant.CODE_SUCCESS) {
                            service_total_size_td = respond.getTotalCount();
                            cellListTD_good = CountryUtil.changeToVillageBean(respond);
                            total_size_td = cellListTD_good.size();
                            isSuccess = true;
                        }
                    } else {

                        CityAreaFtpInfoRespond respond = CountryUtil
                                .getVillageNetDataByCityCodeAdapter(CellInfoActivity.this, true,
                                        cityCode, false, total_size_2g);
                        if (respond != null && respond.getErrorCode() == HttpConstant.CODE_SUCCESS) {
                            cellList2G_good = CountryUtil.changeToVillageBean(respond);
                            service_total_size_2g = respond.getTotalCount();
                            total_size_2g = cellList2G_good.size();
                            isSuccess = true;
                        }
                    }
                }

                if (isSuccess) {
                    handler.sendEmptyMessage(clear);
                }
                // 必须放到cellListTD_good 后面
                initializeShowAllInfo(isFromGrid);

                handler.sendEmptyMessage(0);
            };
        }.start();
    }

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {

            if (msg.what == clear) {
                if (itemlist != null) {
                    itemlist.clear();
                    cityAdapter.notifyDataSetChanged();

                }
            } else if (msg.what == reshow) {
                if (itemlist != null) {
                    itemlist.clear();
                    cityAdapter.notifyDataSetChanged();
                    setLayoutData();
                    setListViewData(isShowTdData);

                }

            } else if (showDialog == msg.what) {
                if (dialog != null && !dialog.isShowing()) {
                    dialog.show();
                }
            } else {

                setLayoutData();
                setListViewData(isShowTdData);
                if (dialog != null && dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        };
    };

    private OnClickListener onclick = new OnClickListener() {
        @SuppressWarnings("unchecked")
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_left:
                    finish();
                    break;
                case R.id.btn_right:
                    if (isShare) {
                        String shareData = setShareData();
                        if (isShareData) {
                            try {
                                MyCommonUtil.share(CellInfoActivity.this,
                                        getString(R.string.share), shareData);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        isShare = false;
                    } else {
                        cbSelect.setChecked(false);
                        if (cityAdapter.getCount() > 0)
                            cityAdapter.setAllSelect(false);
                        isShare = true;
                    }
                    shareLayout();
                    break;
                case R.id.tvSpeed:
                    if (isFre) {
                        Collections.sort(itemlist, new SortCellStringSpeed(SortCellStringSpeed.UP));
                        tvSpeed.setText(getString(R.string.rate) + getString(R.string.sortdown));
                        isFre = false;
                    } else {
                        Collections.sort(itemlist,
                                new SortCellStringSpeed(SortCellStringSpeed.DOWN));
                        tvSpeed.setText(getString(R.string.rate) + getString(R.string.sortup));
                        isFre = true;
                    }

                    tvCellName.setText(R.string.cellName2);
                    // tvCalValue.setText(R.string.detail_cell_lac);
                    tvDropRate.setText(R.string.city_top_slide_drop_rate);
                    tvSignalValue.setText(R.string.city_top_slide_signal);
                    tvjietong.setText(R.string.city_top_slide_call_complete);

                    cityAdapter.notifyDataSetChanged();
                    break;
                case R.id.tvDropRate:
                    if (isDrop) {
                        Collections.sort(itemlist, new SortCellIntSpeed(SortCellIntSpeed.UP,
                                SortCellIntSpeed.droppingRate));
                        tvDropRate.setText(getString(R.string.city_top_slide_drop_rate)
                                + getString(R.string.sortdown));
                        isDrop = false;
                    } else {
                        Collections.sort(itemlist, new SortCellIntSpeed(SortCellIntSpeed.DOWN,
                                SortCellIntSpeed.droppingRate));
                        tvDropRate.setText(getString(R.string.city_top_slide_drop_rate)
                                + getString(R.string.sortup));
                        isDrop = true;
                    }
                    tvCellName.setText(R.string.cellName2);
                    // tvCalValue.setText(R.string.detail_cell_lac);
                    tvSpeed.setText(R.string.speedsl);
                    tvSignalValue.setText(R.string.city_top_slide_signal);
                    tvjietong.setText(R.string.city_top_slide_call_complete);
                    cityAdapter.notifyDataSetChanged();
                    break;
                case R.id.tvSignalValue:
                    if (isSignal) {
                        Collections.sort(itemlist, new SortCellIntSpeed(SortCellIntSpeed.UP,
                                SortCellIntSpeed.signalStrength));
                        tvSignalValue.setText(getString(R.string.city_top_slide_signal)
                                + getString(R.string.sortdown));
                        isSignal = false;
                    } else {
                        Collections.sort(itemlist, new SortCellIntSpeed(SortCellIntSpeed.DOWN,
                                SortCellIntSpeed.signalStrength));
                        tvSignalValue.setText(getString(R.string.city_top_slide_signal)
                                + getString(R.string.sortup));
                        isSignal = true;
                    }
                    tvCellName.setText(R.string.cellName2);
                    // tvCalValue.setText(R.string.detail_cell_lac);
                    tvSpeed.setText(R.string.speedsl);
                    tvDropRate.setText(R.string.city_top_slide_drop_rate);
                    tvjietong.setText(R.string.city_top_slide_call_complete);
                    cityAdapter.notifyDataSetChanged();
                    break;
                case R.id.tvjietong:
                    if (isRand) {
                        Collections.sort(itemlist, new SortCellIntSpeed(SortCellIntSpeed.UP,
                                SortCellIntSpeed.rand));
                        tvjietong.setText(getString(R.string.city_top_slide_call_complete)
                                + getString(R.string.sortdown));
                        isRand = false;
                    } else {
                        Collections.sort(itemlist, new SortCellIntSpeed(SortCellIntSpeed.DOWN,
                                SortCellIntSpeed.rand));
                        tvjietong.setText(getString(R.string.city_top_slide_call_complete)
                                + getString(R.string.sortup));
                        isRand = true;
                    }
                    tvCellName.setText(R.string.cellName2);
                    // tvCalValue.setText(R.string.detail_cell_lac);
                    tvSpeed.setText(R.string.speedsl);
                    tvSignalValue.setText(R.string.city_top_slide_signal);
                    tvDropRate.setText(R.string.city_top_slide_drop_rate);
                    cityAdapter.notifyDataSetChanged();
                    break;
                case R.id.tvCellName:
                    if (isname) {
                        Collections.sort(itemlist, new SortCellIntSpeed(SortCellIntSpeed.UP,
                                SortCellIntSpeed.ci));
                        tvCellName.setText(getString(R.string.cellName2)
                                + getString(R.string.sortdown));
                        isname = false;
                    } else {
                        Collections.sort(itemlist, new SortCellIntSpeed(SortCellIntSpeed.DOWN,
                                SortCellIntSpeed.ci));
                        tvCellName.setText(getString(R.string.cellName2)
                                + getString(R.string.sortup));
                        isname = true;
                    }
                    // tvCalValue.setText(R.string.detail_cell_lac);
                    tvSpeed.setText(R.string.speedsl);
                    tvSignalValue.setText(R.string.city_top_slide_signal);
                    tvDropRate.setText(R.string.city_top_slide_drop_rate);
                    tvjietong.setText(R.string.titleRank);
                    cityAdapter.notifyDataSetChanged();
                    // break;
                    // case R.id.tvCalValue:
                    // if (isCal) {
                    // Collections.sort(itemlist, new
                    // SortCellIntSpeed(SortCellIntSpeed.UP,
                    // SortCellIntSpeed.lac));
                    // tvCalValue.setText(getString(R.string.detail_cell_lac)
                    // + getString(R.string.sortdown));
                    // isCal = false;
                    // } else {
                    // Collections.sort(itemlist, new
                    // SortCellIntSpeed(SortCellIntSpeed.DOWN,
                    // SortCellIntSpeed.lac));
                    // tvCalValue.setText(getString(R.string.detail_cell_lac)
                    // + getString(R.string.sortup));
                    // isCal = true;
                    // }
                    // tvCellName.setText(R.string.cellName2);
                    // tvSpeed.setText(R.string.speedsl);
                    // tvSignalValue.setText(R.string.city_top_slide_signal);
                    // tvDropRate.setText(R.string.city_top_slide_drop_rate);
                    // tvjietong.setText(R.string.titleRank);
                    // cityAdapter.notifyDataSetChanged();
                    // break;
            }
        }

    };

    private String setShareData() {
        StringBuffer sb = new StringBuffer();

        isShareData = false;
        if (cbInfoSelect.isChecked()) {
            isShareData = true;
            if (showAllInfo == null || showAllInfo.equals(""))
                sb.append(getString(R.string.bottom_country));
            else
                sb.append(Html.fromHtml(showAllInfo));
            //
        }
        sb.append("\n");
        // sb.append(setShareString(rbNetTD.isChecked(),rbNetStand.isChecked()));
        // sb.append("\n");
        for (int i = 0; i < itemlist.size(); i++) {
            if (cityAdapter.isSelected(i)) {
                sb.append(itemlist.get(i).getVillage_name());
                sb.append(" ");

                if (isShowTdData) {
                    sb.append(getString(R.string.netTD));
                } else {
                    sb.append(getString(R.string.net2G));
                }
                sb.append(":");
                sb.append(itemlist.get(i).getAverageSpeed());
                sb.append(";");

                sb.append(getString(R.string.detail_cell_ci));
                sb.append(":");
                sb.append(itemlist.get(i).getCi());
                sb.append(";");

                sb.append(getString(R.string.city_top_slide_signal));
                sb.append(":");
                sb.append(itemlist.get(i).getSignalStrength());
                sb.append(";");

                sb.append(getString(R.string.city_top_slide_call_complete));
                sb.append(":");
                sb.append(itemlist.get(i).getJietong());
                sb.append(";");

                sb.append(getString(R.string.city_top_slide_drop_rate));
                sb.append(":");
                sb.append(itemlist.get(i).getDroppingRate());
                sb.append(";");

                sb.append("\n");
            }
        }
        sb.append("--" + getString(R.string.app_name));
        return sb.toString();
    }

    private String setShareString(boolean isTD, boolean isTop) {
        if (isTD && isTop) {
            return "TD网络 最好小区排名Top10";
        } else if (isTD && !isTop) {
            return "TD网络 最差小区排名Top10";
        } else if (!isTD && isTop) {
            return "2G网络 最好小区排名Top10";
        } else {
            return "2G网络 最差小区排名Top10";
        }
    }

    private OnItemClickListener onItemClickListener = new OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            if (isShare) {
                cityAdapter.setRadioSelect(arg2);
            } else {
                ((CheckBox) findViewById(R.id.cbStretch)).setChecked(false);
                arg0.requestFocusFromTouch();
                // arg0.setSelection(arg2);
                arg0.setSelected(true);
                defineDialog.setLayoutData(itemlist.get(arg2));
                defineDialog.show();
            }
        }
    };

    private OnScrollListener scrollListener = new OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub
            switch (view.getId()) {
                case R.id.listViewRank:
                    if (lyStretch.getVisibility() == View.VISIBLE)
                        ((CheckBox) findViewById(R.id.cbStretch)).setChecked(false);
                    break;
            }
        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                int totalItemCount) {
            // TODO Auto-generated method stub

        }
    };

    private OnCheckedChangeListener onCheckedListener = new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            switch (buttonView.getId()) {
                case R.id.cbInfoSelect:
                    break;
                case R.id.cbSelect:
                    if (cityAdapter.getCount() > 0)
                        cityAdapter.setAllSelect(isChecked);
                    break;
                case R.id.rbNetTD:
                case R.id.rbNet2G:
                case R.id.rbNetStand:
                case R.id.rbNetFall:
                    // setListViewData(rbNetTD.isChecked(),rbNetStand.isChecked());
                    break;
                case R.id.cbStretch:
                    if (isChecked) {
                        lyStretch.setVisibility(View.VISIBLE);
                        ly_first.setVisibility(View.GONE);
                    } else {
                        lyStretch.setVisibility(View.GONE);
                        ly_first.setVisibility(View.VISIBLE);
                    }
                    break;
            }
        }
    };

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.ly_layout:
                    if (lyStretch.getVisibility() == View.GONE)
                        ((CheckBox) findViewById(R.id.cbStretch)).setChecked(true);
                    else
                        ((CheckBox) findViewById(R.id.cbStretch)).setChecked(false);
                    break;
                case R.id.allcontent:
                    if (lyStretch.getVisibility() == View.GONE)
                        ((CheckBox) findViewById(R.id.cbStretch)).setChecked(true);
                    else
                        ((CheckBox) findViewById(R.id.cbStretch)).setChecked(false);
                    break;
                case R.id.firstcontent:
                    if (lyStretch.getVisibility() == View.GONE)
                        ((CheckBox) findViewById(R.id.cbStretch)).setChecked(true);
                    else
                        ((CheckBox) findViewById(R.id.cbStretch)).setChecked(false);
                    break;
            }
        }
    };

    @Override
    public void refresh() {
        getData();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (isShare) {
                    isShare = false;
                    shareLayout();
                    return true;
                }
                break;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void addMoreData() {

        if (isShowTdData) {
            if (total_size_td < service_total_size_td || service_total_size_td == 0) {
                total_size_td = total_size_td + every_size;
                getData();
            } else {
                MyCommonUtil.showMessage(CellInfoActivity.this, R.string.data_have_all);
            }
        } else {
            if (total_size_2g < service_total_size_2g || service_total_size_2g == 0) {
                total_size_2g = total_size_2g + every_size;
                getData();
            } else {
                MyCommonUtil.showMessage(CellInfoActivity.this, R.string.data_have_all);
            }
        }

    }

    private OnScrollListener myOnScrollListener = new OnScrollListener() {
        boolean isLastRow = false;

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                int totalItemCount) {
            // 滚动时一直回调，直到停止滚动时才停止回调。单击时回调一次。
            // firstVisibleItem：当前能看见的第一个列表项ID（从0开始）
            // visibleItemCount：当前能看见的列表项个数（小半个也算）
            // totalItemCount：列表项共数

            // 判断是否滚到最后一行
            if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) {
                isLastRow = true;
            }
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // 正在滚动时回调，回调2-3次，手指没抛则回调2次。scrollState = 2的这次不回调
            // 回调顺序如下
            // 第1次：scrollState = SCROLL_STATE_TOUCH_SCROLL(1) 正在滚动
            // 第2次：scrollState = SCROLL_STATE_FLING(2) 手指做了抛的动作（手指离开屏幕前，用力滑了一下）
            // 第3次：scrollState = SCROLL_STATE_IDLE(0) 停止滚动
            // 当滚到最后一行且停止滚动时，执行加载
            if (isLastRow && scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                // 加载元素
                addMoreData();
                isLastRow = false;
            }
        }
    };

}
